﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NOAH.CMS.Repository;
using NOAH.CMS.Cache;

public class CourseController : Controller
{
    //[WhitespaceFilter]
    public ActionResult List(int? id)
    {
        List<Course> courses = new List<Course>();
        List<Subject> subjects = new List<Subject>();
        subjects = new SubjectCacheLayer().GetAll().OrderBy(p => p.Order).ToList();
        string cateTitle = "Tất cả khóa học";

        if (id.HasValue)
        {
            Subject currentSubject = subjects.Find(p => p.ID == id.Value);
            if (currentSubject != null)
            {
                cateTitle = currentSubject.Name;
            }
        }

        if (id.Value == 0)
        {
            courses = new CourseRepository().GetAll().OrderBy(p => p.Order).ToList();
        }
        else
        {
            courses = new CourseRepository().GetCourseBySubject(id.Value).OrderBy(p=>p.Order).ToList();
        }

        ViewBag.Courses = courses;
        ViewBag.Subjects = subjects;
        ViewBag.SubjectTitle = cateTitle;
        ViewBag.Title = cateTitle;
        return View();
    }


    public ActionResult Detail(int id)
    {
        Course course = new Course();
        CourseRepository repository = new CourseRepository();
        course = repository.Get(id);
        ViewBag.Title = course.Name;
        return View(course);
    }

}
